package info.pandd.centrumDowodzeniaSMS;

import info.pandd.centrumDowodzeniaSMS.SMSHistoryProviderMetaData.SMSTableMetaData;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.ListActivity;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class HistoriaSMSActivity extends ListActivity{
	
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.history_list);
		
		Button powrot = (Button) findViewById(R.id.goBack);
		powrot.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		//sekcja pobrania historii wysłanych SMSów
		ArrayList<String> wyslane = new ArrayList<String>();
		
		final ListView lvWiadomosci = (ListView) findViewById(android.R.id.list);
		String[] proj = new String[]{SMSHistoryProviderMetaData.SMSTableMetaData._ID,
									SMSTableMetaData.MSG_RECEIVER_NAME,
									SMSTableMetaData.MSG_RECEIVER_NUMBER,
									SMSTableMetaData.MSG_TEXT,
									SMSTableMetaData.MSG_DATE,
									};
		
		Cursor c = managedQuery(SMSTableMetaData.CONTENT_URI, proj, null, null, null);
		if(c.getCount() == 0){
			Toast msg = Toast.makeText(getApplicationContext(), "Historia wysłanych jest pusta!", Toast.LENGTH_SHORT);
			msg.show();
			finish();
		}
		else{
			while(c.moveToNext()){
				String wiadomosc = c.getString(1) + " (" + c.getString(2)+ ")\n" + milisToPolishDate(Long.parseLong(c.getString(4)));
				wyslane.add(wiadomosc);
			}
			
		final ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, wyslane);
		
		lvWiadomosci.setAdapter(aa);
		}
		//koniec historii
		
		lvWiadomosci.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
	            String[] proj = new String[]{
	            		SMSTableMetaData.MSG_RECEIVER_NAME,
						SMSTableMetaData.MSG_RECEIVER_NUMBER,
						SMSTableMetaData.MSG_TEXT,
						SMSTableMetaData.MSG_DATE,
						};
	            Uri uri = ContentUris.withAppendedId(SMSTableMetaData.CONTENT_URI, lvWiadomosci.getCount()-arg2);
	            Cursor c = managedQuery(uri, proj, null, null, null);
	            if(c.getCount() == 0){
	    			Toast msg = Toast.makeText(getApplicationContext(), "Coś się nie udało :/", Toast.LENGTH_SHORT);
	    			msg.show();
	    		}
	            else{
	            	c.moveToNext();
	            	Alerts.showSentMessageBox(c.getString(0), c.getString(1), c.getString(2), milisToPolishDate(Long.parseLong(c.getString(3))), HistoriaSMSActivity.this);
	            }
			}
		});
	}
	
	
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_historii_sms, menu);
        return true;
    }
	
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	if(item.getItemId() == R.id.clearHistory){
    		ContentResolver cr = getContentResolver();
    		Uri uri = SMSTableMetaData.CONTENT_URI;
    		cr.delete(uri, null, null);
    		Toast msg = Toast.makeText(getApplicationContext(), "Historia wysłanych wiadomości została wyczyszczona.", Toast.LENGTH_LONG);
    		msg.show();
    		finish();
    	}
    	return true;
    }
    
    //metoda formatująca datę z milisekund do formy ogólnie przyjętej w Polsce
    private String milisToPolishDate(long milisekundy){
    	String pattern = "dd.MM.yyyy' 'HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern(pattern);
        
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(milisekundy);
		java.util.Date date = cal.getTime();
    	return sdf.format(date);
    }
}